package com.gax.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gax.pojo.Headline;
import com.gax.service.HeadlineService;
import com.gax.mapper.HeadlineMapper;
import com.gax.utils.JwtHelper;
import com.gax.utils.Result;
import com.gax.vo.PortalVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 19115
* @description 针对表【news_headline】的数据库操作Service实现
* @createDate 2025-06-16 19:50:31
*/
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline>
    implements HeadlineService{
    @Autowired
    private HeadlineMapper headlineMapper;

    @Autowired
    private JwtHelper jwtHelper;

    // 进行分页数据查询，查询后，将分页数据加入到 result
    // 需要自定义 Mapper 方法，并且携带分页信息
    // 返回的结果应该是 List<Map>
    @Override
    public Result findNewsPage(PortalVo portalVo) {
        IPage<Map> page = new Page<>(portalVo.getPageNum(), portalVo.getPageSize());
        headlineMapper.selectMyPage(page, portalVo);   // page 已被填充

        List<Map> records = page.getRecords();         // ① 取分页数据
        HashMap map1 = new HashMap<>();
        map1.put("pageData", records);                 // ② 手动塞进 pageData
        map1.put("pageNum", page.getCurrent());
        map1.put("pageSize", page.getSize());
        map1.put("totalPage", page.getPages());
        map1.put("totalSize", page.getTotal());

        Map pageInfo = new HashMap<>();
        pageInfo.put("pageInfo", map1);                // ③ 再包一层 pageInfo

        return Result.ok(pageInfo);                    // ④ 外层 Result 再包 code/message
    }

    // 查询详情
    @Override
    public Result showHeadlineDetail(Integer hid) {
        // 多表联查，查询出对应的数据
        Map data = headlineMapper.queryDetailMap(hid);
        HashMap<String, Object> headlineMap = new HashMap<>();
        headlineMap.put("headline", data);
        // 修改阅读量 ＋ 1
        Headline headline = new Headline();
        headline.setHid((Integer) data.get("hid"));
        headline.setVersion((Integer) data.get("version"));
        // 阅读量 ＋ 1
        headline.setPageViews((Integer) data.get("pageViews") + 1);
        headlineMapper.updateById(headline);
        return Result.ok(headlineMap);
    }

    @Override
    public Result publish(Headline headline,String token) {
        // token 查询用户 id
        int userId = jwtHelper.getUserId(token).intValue();
        // 数据装配
        headline.setPublisher(userId);
        headline.setPageViews(0);
        headline.setCreateTime(new Date());
        headline.setUpdateTime(new Date());
        headlineMapper.insert(headline);
        return Result.ok(null);
    }

    @Override
    public Result updateData(Headline headline) {
        // 修改头条数据，根据 hid 查询数据的最新 version
        Integer version = headlineMapper.selectById(headline.getHid()).getVersion();
        // 设置最新的 version
        headline.setVersion(version);
        headline.setUpdateTime(new Date());
        headlineMapper.updateById(headline);
        return Result.ok(null);
    }
}